package com.zhaoyu.article.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhaoyu.article.mapper.ApArticleConfigMapper;
import com.zhaoyu.article.mapper.ApArticleContentMapper;
import com.zhaoyu.article.mapper.ApArticleMapper;
import com.zhaoyu.article.service.ApArticleService;
import com.zhaoyu.common.constants.ArticleConstants;
import com.zhaoyu.model.article.dto.ArticleHomeDto;
import com.zhaoyu.model.article.dto.ArticleInfoDto;
import com.zhaoyu.model.article.pojos.ApArticle;
import com.zhaoyu.model.article.pojos.ApArticleConfig;
import com.zhaoyu.model.article.pojos.ApArticleContent;
import com.zhaoyu.model.common.dtos.ResponseResult;
import com.zhaoyu.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * IService 实现类（泛型：M 是 mapper 对象，T 是实体）
 */
@Service
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements ApArticleService {

    @Autowired
    private ApArticleMapper apArticleMapper;

    @Autowired
    private ApArticleConfigMapper apArticleConfigMapper;

    @Autowired
    private ApArticleContentMapper apArticleContentMapper;

    @Value("${fileServerUrl}")
    private String fileServerUrl; // 图片服务器地址

    @Override
    public ResponseResult load(Short loadType, ArticleHomeDto articleHomeDto) {
        // 检查参数
        Integer size = articleHomeDto.getSize();
        if (size == null || size == 0){
            size = 10;
        }
        articleHomeDto.setSize(size);
        // 类型参数检验
        if (!loadType.equals(1) && !loadType.equals(2)){
            loadType = 1;
        }
        // 文章频道校验
        if (StringUtils.isEmpty(articleHomeDto.getTag())){
            articleHomeDto.setTag(ArticleConstants.DEFAULT_TAG);
        }
        // 时间校验
        if (articleHomeDto.getMinBehotTime() == null){
            articleHomeDto.setMinBehotTime(new Date());
        }
        if (articleHomeDto.getMaxBehotTime() == null){
            articleHomeDto.setMaxBehotTime(new Date());
        }
        // 查询数据
        List<ApArticle> apArticles = apArticleMapper.loadArticleList(articleHomeDto,loadType);
        // 结果封装
        ResponseResult responseResult = ResponseResult.okResult(apArticles);
        responseResult.setHost(fileServerUrl);
        return responseResult;
    }

    @Override
    public ResponseResult loadApArticleInfo(ArticleInfoDto articleInfoDto) {
        Map<String,Object> resultMap = new HashMap<>();
        // 检查参数
        if (articleInfoDto == null || articleInfoDto.getArticleId() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        // 查询文章配置
        ApArticleConfig apArticleConfig = apArticleConfigMapper.selectOne(Wrappers.<ApArticleConfig>lambdaQuery().eq(ApArticleConfig::getArticleId, articleInfoDto.getArticleId()));
        if (apArticleConfig == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 查询文章内容
        if (!apArticleConfig.getIsDelete() && !apArticleConfig.getIsDown()){ // isDelete：是否已删除 isDown：是否下架
            ApArticleContent apArticleContent = apArticleContentMapper.selectOne(Wrappers.<ApArticleContent>lambdaQuery().eq(ApArticleContent::getArticleId, articleInfoDto.getArticleId()));
            resultMap.put("content",apArticleContent);
        }
        resultMap.put("config",apArticleConfig);
        return ResponseResult.okResult(resultMap);
    }

}